﻿namespace Jim.K3Cloud;

/// <summary>
///  金蝶云星空类型转换
/// </summary>
public class TypeConverter: JsonConverter
{
    public override bool CanRead => true;

    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
    {
        string value = string.Empty;
        try
        {
            JToken token = JToken.Load(reader);
            if(token != null)
            {
                Type type= token.ToObject<Type>();
                if(type== null) value= "";
                else if(type== typeof(Int64)) value= "long";
                else if(type== typeof(Int32)) value= "int";
                else if(type== typeof(String)) value= "string";
                else value= type.Name;
            }
            return value;
        }
        catch (Exception e)
        {
            Console.WriteLine($"json转换错误:{e.Message}");
        }
        return value;
    }

    public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
    {
        writer.WriteValue(value);
    }
}